package com.rede.didiok.admin.restapi;


import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rede.didiok.admin.entity.LinkEntity;
import com.rede.didiok.admin.service.LinkService;
import com.rede.didiok.admin.service.SysParamsService;
import com.rede.didiok.admin.service.WebConfigService;
import com.rede.didiok.admin.service.WebNavbarService;
import com.rede.didiok.common.annotion.log.BussinessLog;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.enums.BehaviorEnum;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.RedisUtil;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;

/**
 * 首页 RestApi
 *
 * @author 陌溪
 * @since 2018-09-04
 */
@RestController
@RequestMapping("/index")
@Api(value = "首页相关接口", tags = {"首页相关接口"})
@Slf4j
public class WebIndexRestApi {

    @Resource
    private LinkService linkService;
    @Resource
    private WebConfigService webConfigService;
    @Resource
    private SysParamsService sysParamsService;
    @Resource
    private WebNavbarService webNavbarService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @ApiOperation(value = "获取网站配置", notes = "获取友情链接")
    @GetMapping("/getWebConfig")
    public String getWebConfig() {
        log.info("获取网站配置");
        return ResultUtil.result(SysConf.SUCCESS, webConfigService.getWebConfigByShowList());
    }

    @ApiOperation(value = "判断是否开启小程序审核模式", notes = "判断是否开启小程序审核模式")
    @GetMapping("/getOpenMobileComment")
    public String getOpenMobileComment() {
        log.info("判断是否开启小程序审核模式");
        return ResultUtil.result(SysConf.SUCCESS, webConfigService.getOpenMobileComment());
    }

    @ApiOperation(value = "获取网站导航栏", notes = "获取网站导航栏")
    @GetMapping("/getWebNavbar")
    public String getWebNavbar() {
        log.info("获取网站导航栏");
        return ResultUtil.result(SysConf.SUCCESS, webNavbarService.getAllList());
    }

    @BussinessLog(value = "记录访问页面", behavior = BehaviorEnum.VISIT_PAGE)
    @ApiOperation(value = "记录访问页面", notes = "记录访问页面")
    @GetMapping("/recorderVisitPage")
    public String recorderVisitPage(@ApiParam(name = "pageName", value = "页面名称", required = false) @RequestParam(name = "pageName", required = false) String pageName) {

        if (StringUtils.isEmpty(pageName)) {
            return ResultUtil.result(SysConf.SUCCESS, MessageConf.PARAM_INCORRECT);
        }
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.INSERT_SUCCESS);
    }

    @ApiOperation(value = "获取友情链接", notes = "获取友情链接")
    @GetMapping("/getLink")
    public String getLink() {
        String friendlyLinkCount = sysParamsService.getSysParamsValueByKey(SysConf.FRIENDLY_LINK_COUNT);
        // 从Redis中获取友情链接
        String jsonResult = redisUtil.get(RedisConf.RANK_MEMBER_LINK + Constants.SYMBOL_COLON + friendlyLinkCount);
        if (StringUtils.isNotEmpty(jsonResult)) {
            List jsonResult2List = JsonUtils.jsonArrayToArrayList(jsonResult);
            return ResultUtil.result(SysConf.SUCCESS, jsonResult2List);
        }
        List<LinkEntity> linkList = linkService.getListByPageSize(Integer.valueOf(friendlyLinkCount));
        if (linkList.size() > 0) {
            redisUtil.setEx(RedisConf.RANK_MEMBER_LINK + Constants.SYMBOL_COLON + friendlyLinkCount, JsonUtils.objectToJson(linkList), 1, TimeUnit.HOURS);
        }
        return ResultUtil.result(SysConf.SUCCESS, linkList);
    }

    @BussinessLog(value = "点击友情链接", behavior = BehaviorEnum.FRIENDSHIP_LINK)
    @ApiOperation(value = "增加友情链接点击数", notes = "增加友情链接点击数")
    @GetMapping("/addLinkCount")
    public String addLinkCount(@ApiParam(name = "uid", value = "友情链接UID", required = false) @RequestParam(name = "uid", required = false) String uid) {
        log.info("点击友链");
        return linkService.addLinkCount(uid);
    }

    /**
     * 获取版本参数
     * @return
     */
    @GetMapping("/getVersionParams")
    public String getVersionParams() {
        String versionParams = sysParamsService.getSysParamsValueByKey(SysConf.SYS_VERSION_PARAMS);
        return ResultUtil.successWithData(versionParams);
    }
}

